package org.gen.core;

import java.io.File;
import java.util.List;

import org.gen.config.GenConfig;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;

/**
 *
 * @author 朱良才
 * @Email zhuliangcai@itcast.com
 * @Date 2017-10-16
 */
public class GenerateCore extends Generate {
    //添加模板需要的数据
    @Override
    protected void rebuildConfigRoot(String table_name, List<Attribute> attr_list) {
        //freemarker 的数据容器 添加属性集合
        root.put("attrs", attr_list);
        //判断是否包含日期，包含则添加import java.util.Date; 否则不添加
        if (hasDateType(attr_list)) {
            root.put("hasDateType", new Object());
        } else {
            root.put("hasDateType", null);
        }
        //freemarker 的数据容器 添加表名
        root.put("className", tableNameToClassName(table_name));
        root.put("tableName",table_name);
    }
    @Override
    public void gen() throws Exception{
        File tempdir = new File(moduleRootPath + "/src/main/resources/templates/");
        Configuration cfg = new Configuration(Configuration.getVersion());
        cfg.setDirectoryForTemplateLoading(tempdir);
        cfg.setDefaultEncoding("UTF-8");
        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
        genpojo(cfg);
        genMapper(cfg);
        genMapperXML(cfg);
        genMapperTest(cfg);

    }


    //生成mapper接口测试类的方法
	private void genMapperTest(Configuration cfg) throws Exception {
		Template template = cfg.getTemplate("mapper.test.java.ftl"); // load E:/Work/Freemarker/templates/person.ftl
		String packageDir = packageToDir(GenConfig.getMappertestPackageName());
        String fileName = root.get("className") + "MapperTest.java";
		commonGenerateFile(packageDir, fileName, template,true);
	}

    //生成mapper接口的方法
    private void genMapper(Configuration cfg ) throws Exception{
        Template template = cfg.getTemplate("mapper.java.ftl");  // load E:/Work/Freemarker/templates/person.ftl
        String packageDir = packageToDir(GenConfig.getMapperPackageName());
        String fileName = root.get("className") + "Mapper.java";
        commonGenerateFile(packageDir ,fileName,template);

    }
    //生成mapper.xml的方法
    private void genMapperXML(Configuration cfg ) throws Exception{
        Template template = cfg.getTemplate("mapper.xml.ftl");  // load E:/Work/Freemarker/templates/person.ftl
        String packageDir = packageToDir(GenConfig.getMapperPackageName());
        String fileName = root.get("className") + "Mapper.xml";
        commonGenerateFile(packageDir ,fileName,template);
    }
    //生成pojo的方法
    private void genpojo(Configuration cfg ) throws Exception{
        Template template = cfg.getTemplate("pojo.java.ftl");  // load E:/Work/Freemarker/templates/person.ftl
        String packageDir = packageToDir(GenConfig.getPackageName());
        String fileName = root.get("className") + ".java";
        commonGenerateFile(packageDir ,fileName,template);
    }

}
